package com.futurelight.recipes.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.futurelight.recipes.R;
import com.futurelight.recipes.adapter.CallBackResult;
import com.futurelight.recipes.adapter.CandidateAdapter;
import com.futurelight.recipes.adapter.FavoriteAdapter;
import com.futurelight.recipes.dao.EntityFavorite;
import com.futurelight.recipes.model.FavoriteViewModel;
import com.futurelight.recipes.model.Result;
import com.futurelight.recipes.ui.activity.DetailActivity;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class FavoriteFragment extends Fragment {

    private View rootView;
    private FavoriteViewModel favoriteViewModel;
    private RecyclerView recyclerView;
    private FavoriteAdapter favoriteAdapter;
    private SearchView searchView;
    private ListView candidate;
    private CandidateAdapter candidateAdapter;
    private SharedPreferences sharedPref;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_favorite, container, false);
        Log.v("ddd", "onCreateView");
        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        return rootView;
    }

    void findView() {
        recyclerView = rootView.findViewById(R.id.favorite_recycler_view);
        searchView = rootView.findViewById(R.id.search);
        candidate = rootView.findViewById(R.id.candidate);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        findView();
        initFavorite();
        initSearch();

        Log.v("ddd", "onViewCreated");

    }
    void initFavorite(){
        favoriteViewModel = new ViewModelProvider(this,
                (ViewModelProvider.Factory) ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication()))
                .get(FavoriteViewModel.class);
        favoriteAdapter = new FavoriteAdapter(recyclerView);
        recyclerView.setAdapter(favoriteAdapter);
        favoriteViewModel.getData().observe(getViewLifecycleOwner(), new Observer<List<Result>>() {
            @Override
            public void onChanged(List<Result> results) {
                favoriteAdapter.setResultList(results);
            }
        });

        favoriteViewModel.getCandidates().observe(getViewLifecycleOwner(), new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                candidateAdapter.setCandidate(strings);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        favoriteAdapter.setCallBack(new CallBackResult() {
            @Override
            public void callBackResult(Result result) {
                Intent intent = new Intent(getContext(), DetailActivity.class);
                intent.putExtra("result", result);
                startActivity(intent);
                requireActivity().overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }

            @Override
            public void deleteCallBack(Result result) {
                favoriteViewModel.deleteFavorite(new EntityFavorite(result.id));
            }

            @Override
            public void callText(String s) {
            }
        });


    }

    private void initSearch() {
        searchView.setSubmitButtonEnabled(true);
        searchView.setQueryRefinementEnabled(true);
        candidateAdapter = new CandidateAdapter();
        candidateAdapter.setCandidate(new ArrayList<>(sharedPref.getAll().keySet()));
        candidate.setAdapter(candidateAdapter);
        candidate.setVisibility(View.GONE);
        searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    candidate.setVisibility(View.VISIBLE);
                } else {
                    candidate.setVisibility(View.GONE);
                }
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Log.v("ddd", "search" + query);
//                sharedPref.edit().putString(query, query);
                favoriteViewModel.search(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        candidateAdapter.setCallBack(new CallBackResult() {
            @Override
            public void callBackResult(Result result) {
            }

            @Override
            public void deleteCallBack(Result result) {
            }

            @Override
            public void callText(String s) {
                searchView.setQuery(s, false);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.v("ddd", "onStart");
        favoriteViewModel.getFavoriteResults();
    }

}